from collections import defaultdict

n, m = map(int, input().split())
board = []
for _ in range(n):
    board.append(list(map(int, input().split())))

hash_map = defaultdict(lambda: defaultdict(int))
for i in range(n):
    for j in range(m):
        b1 = i - j
        b2 = i + j
        val = board[i][j]
        hash_map[(b1, 1)][val] += 1
        hash_map[(b2, -1)][val] += 1
ans = 0
for vals in hash_map.values():
    for val in vals.values():
        ans += val * (val - 1)
print(ans)
